package org.gd136.ce.script.impl;

import org.gd136.ce.script.Script;
import org.gd136.ce.script.ScriptStage;

public class JumpScript extends Script {

	String partName;

	private static final String TAG = "#jump ";

	@Override
	public Script readScript(String scriptLine) {
		JumpScript js = null;
		if (scriptLine.startsWith(TAG)) {
			js = new JumpScript();
			js.partName = scriptLine.substring(TAG.length());
			if (js.partName.length() < 1) {
				throw new IllegalArgumentException("jump 需要有效的partName. at["
						+ fileName + "] line[" + lineNum + "]");
			}
			js.addToPoolAndLinkFrontScript();
		}
		return js;
	}

	@Override
	public void execute(ScriptStage stage) {
		if (!SM.pool.containsKey(partName)) {
			System.err.println("Jump Faild.未能找到 [" + partName + "]的定义.");
		} else {
			SM.currentPart = SM.pool.get(partName);
			SM.currentPartName = partName;
			SM.stack.clear();
			SM.stack.add(SM.currentPart.get(0));
			SM.getCurrentScript().execute(stage);
		}
	}

}
